using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using NHibernate;
using NHibernate.Linq;

namespace Infrastucture
{
    public class InventoryWarehouseRepository : BaseNHibernateRepository<InventoryWarehouse>
    {
        public InventoryWarehouseRepository(ISessionStorage sessionStorage)
            : base(sessionStorage)
        {
        }

        public InventoryWarehouseRepository(ISessionStorage sessionStorage, ISession session)
            : base(sessionStorage, session)
        {
        }

        public override IList<InventoryWarehouse> GetDataList(Expression<Func<InventoryWarehouse, bool>> Criteria)
        {
            IList<InventoryWarehouse> Result = new List<InventoryWarehouse>();
            using (ITransaction tx = Session.BeginTransaction())
            {
                Result = Session.Linq<InventoryWarehouse>().Where(Criteria).ToList();
                tx.Commit();
            }

            return Result;
        }

        public override void SaveOrUpdate(InventoryWarehouse inventoryWarehouse)
        {
            var sb = new StringBuilder();

            if (inventoryWarehouse.Qty == 0)
                Session.Delete(inventoryWarehouse);
            else
            {
                Session.SaveOrUpdate(inventoryWarehouse);
                //if (inventoryWarehouse.InventoryWarehouseId != 0)
                //{

                //    sb.Append("Update InventoryWarehouse set ");
                //    sb.Append("SerialNumber='" + inventoryWarehouse.SerialNumber + "',");
                //    sb.Append("KodeWarehouse='" + inventoryWarehouse.KodeWarehouse + "',");
                //    sb.Append("Qty='" + inventoryWarehouse.Qty + "',");
                //    sb.Append("Sheet='" + inventoryWarehouse.Sheet + "' ");
                //    sb.Append(" Where InventoryWarehouseId='" + inventoryWarehouse.InventoryWarehouseId + "'");
                //    var query = Session.CreateSQLQuery(sb.ToString());
                //    query.ExecuteUpdate();
                //}else
                //{
                //    sb.Append("Insert Into InventoryWarehouse(");
                //    sb.Append("'" + inventoryWarehouse.InventoryWarehouseId + "',");
                //    sb.Append("'" + inventoryWarehouse.SerialNumber + "',");
                //    sb.Append("'" + inventoryWarehouse.KodeWarehouse + "',");
                //    sb.Append("'" + inventoryWarehouse.Qty + "',");
                //    sb.Append("'" + inventoryWarehouse.Sheet + "')");
                //    var query = Session.CreateSQLQuery(sb.ToString());
                //    query.ExecuteUpdate();
                //}
            }
           
        }

        public override void Delete(InventoryWarehouse inventoryWarehouse)
        {
            ISQLQuery command =
              Session.CreateSQLQuery("Delete from InventoryWarehouse where SerialNumber='" + inventoryWarehouse.SerialNumber + "' and KodeWarehouse='" + inventoryWarehouse.KodeWarehouse + "'");
            int res = command.ExecuteUpdate();
        }
    }
}